package com.cloudtasks;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;

import com.cloudtasks.shared.CloudTasksRequestFactory;
import com.cloudtasks.shared.ItemProxy;
import com.cloudtasks.shared.TaskProxy;
import com.cloudtasks.shared.TaskRequest;
import com.google.web.bindery.requestfactory.shared.Receiver;


public class AsyncSendItem extends AsyncTask<String, Void, Long> {

    private final UsersDisplayActivity activity;
    private boolean newTask = false;
    long id;
    
    public AsyncSendItem(UsersDisplayActivity usersDisplayActivity) {
        super();
        this.activity = usersDisplayActivity;
    }

    @Override
    protected Long doInBackground(String... arguments) {
       // final List<TaskProxy> list = new ArrayList<TaskProxy>();
       

        // INSERT RPC HERE!
        CloudTasksRequestFactory factory = Util.getRequestFactory(activity,
        		CloudTasksRequestFactory.class);
        
        TaskRequest request = factory.taskRequest();
        ItemProxy my_task = request.create(ItemProxy.class);
        
        my_task.setName(arguments[0]);
        my_task.setListId(Long.valueOf(arguments[1]).longValue());
        boolean b = (Long.valueOf(arguments[2]).longValue() == 1);  //1 is checked
        my_task.setchecked(b);
        my_task.setType(arguments[3]);
        
      //  if (arguments.length == 0 || arguments[0] == -1) 
        //{
	        //System.out.println("I'm here...AsyncSend");
	        //Send new user
        	request.updateItem(my_task).fire(new Receiver<ItemProxy>() {
				@Override
				public void onSuccess(ItemProxy arg0) {
					//list.addAll(arg0);
					System.out.println("I'm here...AsyncSend");
					id = arg0.getId();
					//my_task.setId(arg0.getId());
				}
	        });
      //  } else {
        //	newTask = true;
        	//Reading in new user for some reason
        	//factory.taskRequest().readTask(arguments[0]).fire(new Receiver<TaskProxy>() {

				//@Override
				//public void onSuccess(TaskProxy arg0) {
					//list.add(arg0);
				//}
			//});
        //}
       // my_task.setId(id);
        return id;
    }

    @Override
    protected void onPostExecute(Long result) {
    	//if (newTask) {
    		//activity.compare_users(result);
    	//} else {
    		activity.update_item(result);
    		activity.sync_count.decrement();
    	//}
    }
}
